import { CommonComponent } from '../../../../core/common/common.component';
import { CommonService } from '../../../../core/common/common.service';
import { ConfigService } from '../../../../core/common/config.service';
import { TabsetComponent } from '../../../../shared/component/ngx-bootstrap';
import { Component, EventEmitter, OnInit, ViewChild, Input, Output, forwardRef } from '@angular/core';
import { NG_VALUE_ACCESSOR, NG_VALIDATORS } from '@angular/forms';
import { Http, Response } from '@angular/http';
import { TranslateService } from '@ngx-translate/core';
@Component({
  selector: 'ncloud-node',
  template: `
        <form #valForm="ngForm">
             <tabset #nodeTab [plus]="true" (addTabClick)="addTabClick()" [selectNew]="true" [onTabClick]="onTabClick.bind(this)" 
                  [afterTabClick]="afterTabClick.bind(this)">
             <tab *ngFor="let node of data;let i = index;"
                [heading]="node.nodeName" [active]="node.active"
                (select)="addNode(node)" (deselect)="deselect(node)"
                [disabled]="node.disabled" [removable]="node.removable"
                (removed)="removeTabHandler(node)" [customClass]="node.customClass">
                <ncloud-nodecell (calculated)="calculatedFee($event)" required name="{{cloudNodeName(i)}}" 
                  ngModel [(ngModel)]="data[i]"></ncloud-nodecell>
            </tab> 
            </tabset>
</form>
`,
  styleUrls: ['./node.component.scss'],
  providers: [
    { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NodeComponent), multi: true },
    { provide: NG_VALIDATORS, useExisting: forwardRef(() => NodeComponent), multi: true }
  ]
})
export class NodeComponent extends CommonComponent implements OnInit {
  @ViewChild('nodeTab') nodeTab: TabsetComponent;
  public discnt: any;
  private baseDiscnt: any;
  @Input() public product: any;
  @Output() public addDiscnt: EventEmitter<any> = new EventEmitter();
  @Output() public deleteDiscnt: EventEmitter<any> = new EventEmitter();
  @Output() public calculated: EventEmitter<any> = new EventEmitter();
  constructor(private common: CommonService, private http: Http, private config: ConfigService, public translate: TranslateService) {
    super();
    this.data = this.data || [];
    this.onTabClick.bind(this);
  }
  ngOnInit() {
    super.ngOnInit();
  }
  public cloudNodeName(idx): string {
    return 'ncloud-nodecell' + idx;
  }
  public addNode(tab): void { }
  public onTabClick(tabz) {
    return true;
  }
  public addTabClick(): void {
    const d: any = {};
    const node: any = {
      effFlag: '1', removable: true, modifyTag: '1', active: true, cloud: { ipseg: [{}] },
      equip: {}, isp: { wan: {}, lan: { accessType: '1' } }
    };
    this.translate.get('crm.component.node.newNode').subscribe((res: string) => {
      node.nodeName = res;
    });
    if (this.discnt) {
      node.bandWidth = this.discnt.compCount.bandWidth;
      node.discntType = this.discnt.discntType;
      node.discntId = this.discnt.discntId;
      node.disabledAttr = 'bandWidth';
      node.maxFee = this.discnt.maxFee;
      node.minFee = this.discnt.minFee;
      node.cycleUnit = this.discnt.cycleUnit;
      this.common.copy(d, this.discnt);
      d.index = this.data.length + 1;
      node.index = d.index;
      this.addDiscnt.emit(d);
      this.data.push(node);
    } else {
      if (!this.baseDiscnt) {
        const url = this.config.get('url') + '?service=DiscntService&method=getBaseDiscntByProd';
        this.http.post(url, { prodId: this.product.prodId }).toPromise().then((response: Response) => {
          const result = response.json();
          if (result && result.code === '0000') {
            this.baseDiscnt = result.info;
            node.discntType = this.baseDiscnt.discntType;
            node.discntId = this.baseDiscnt.discntId;
            node.cycleUnit = this.baseDiscnt.cycleUnit;
            this.common.copy(d, this.baseDiscnt);
            d.index = this.data.length + 1;
            node.index = d.index;
            this.addDiscnt.emit(d);
            this.data.push(node);
          }
        });
      } else {
        node.discntType = this.baseDiscnt.discntType;
        node.discntId = this.baseDiscnt.discntId;
        node.cycleUnit = this.baseDiscnt.cycleUnit;
        this.common.copy(d, this.baseDiscnt);
        d.index = this.data.length + 1;
        node.index = d.index;
        this.addDiscnt.emit(d);
        this.data.push(node);
      }
    }


  }
  public afterTabClick(tabz) {
  }
  public removeTabHandler(node): void {
    const idx = this.data.indexOf(node);
    this.data.splice(idx, 1);
    this.deleteDiscnt.emit(node.index);
  }
  public deselect(node): void {
    if (node) {
      node.active = false;
    }
  }
  public calculatedFee(standard: number) {
    this.calculated.emit(standard);
  }
}
